import Scanner from './Scanner'
import nestTokens from './nestTokens'

export default function parseTemplateToTokens(templateStr) {
    let scanner = new Scanner(templateStr)
    let tokens = []

    while(scanner.pos != -1) {
        let text = scanner.scanUtil("{{")

        tokens.push([ 'text', text ])

        if(scanner.pos != -1) {            
            scanner.scan("{{")
            let name = scanner.scanUtil("}}")
            name = name.trim()

            if(name[0] == '#') {
                tokens.push([ '#', name.slice(1) ])
            }else if(name[0] == '/' ) {
                tokens.push([ '/', name.slice(1) ])
            }else {
                tokens.push([ 'name', name ])
            }
            
            scanner.scan("}}")
        }
    }
    return nestTokens(tokens)
}